<?php
namespace app\admin\controller;
use think\Db;
use think\Validate;
use think\Request;
use util\Tree;
use app\admin\model\Article as ArticleModel;
use app\admin\model\Topic as TopicModel;
use app\admin\model\ArticleCate as ArticleCateModel;
/**
 * 行为管理控制器
 * @package app\admin\controller
 */
class Article extends Base
{
    /**
     * 首页
     * @author @author 唐小虎 <1731050423@qq.com>
     * @return mailparse_determine_best_xfer_encoding(fp)
     */
    public function index()
    {

        // 数据列表
        $data_list = ArticleModel::order('publish_time desc,id desc')->paginate();
        cookie('__forward__', $_SERVER['REQUEST_URI']);

        $this->assign('meta_title', '文章管理');
        $this->assign('list', $data_list);
        return $this->fetch();
    }

    /**
     * 新增
     * @author 唐小虎 <1731050423@qq.com>
     * @return mixed|void
     */
    public function add()
    {
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Article');
            // 验证失败 输出错误信息
            if(true !== $result) return $this->error($result);
            // 添加数据
            if ($article = ArticleModel::create($data)) {
                // 记录行为
                action_log('article_update', 'article', $article['id'], UID);
                return $this->success('新增成功', cookie('__forward__'));
            } else {
                return $this->error('新增失败');
            }
        }
        $this->assign('cate',ArticleCateModel::getTree(null, '选择分类'));
        $this->assign('meta_title', '新增文章');
        return $this->fetch();
    }

    public function edit($id = '0')
    {
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Article');
            // 验证失败 输出错误信息
            if (true !== $result) {
                return $this->error($result);
            }

            if ($article = ArticleModel::update($data)) {               
                // Cache::clear();
                // 记录行为
                action_log('article_update', 'article', $article['id'], UID);
                $this->success('操作成功', cookie('__forward__'));
            } else {
                $this->error('操作失败', cookie('__forward__'));
            }
        }
        $this->assign('meta_title', '编辑文章');
        $this->assign('cate',ArticleCateModel::getTree(null, '选择分类'));
        $info = ArticleModel::get($id);
        $this->assign('info', $info);

        return $this->fetch();
    }

    /**
     * 首页
     * @author @author 唐小虎 <1731050423@qq.com>
     * @return mailparse_determine_best_xfer_encoding(fp)
     */
    public function category()
    {
        
        // 数据列表
        $data_list = ArticleCateModel::order('sort,id desc')->paginate();
        $data_list = Tree::config()->toList($data_list);
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        $this->assign('meta_title', '文章分类管理');
        $this->assign('list', $data_list);
        return $this->fetch();
    }

    /**
     * 新增
     * @author 唐小虎 <1731050423@qq.com>
     * @return mixed|void
     */
    public function add_category()
    {
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'ArticleCate');
            // 验证失败 输出错误信息
            if(true !== $result) return $this->error($result);
            // 添加数据
            if ($cat = ArticleCateModel::create($data)) {
                // 记录行为
                action_log('articleCate_update', 'article_category', $cat['id'], UID);
                return $this->success('新增成功', cookie('__forward__'));
            } else {
                return $this->error('新增失败');
            }
        }
        $this->assign('cate',ArticleCateModel::getTree(null, '顶级分类'));
        $this->assign('meta_title', '新增文章分类');
        return $this->fetch();
    }

    public function edit_category($id = '0')
    {
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'ArticleCate');
            // 验证失败 输出错误信息
            if (true !== $result) {
                return $this->error($result);
            }

            if ($cat = ArticleCateModel::update($data)) {               
                // Cache::clear();
                // 记录行为
                action_log('articleCate_update', 'article_category', $cat['id'], UID);
                $this->success('操作成功', cookie('__forward__'));
            } else {
                $this->error('操作失败', cookie('__forward__'));
            }
        }
        $this->assign('cate',ArticleCateModel::getTree(null, '顶级分类'));
        $this->assign('meta_title', '编辑文章分类');
        $info = ArticleCateModel::get($id);
        $this->assign('info', $info);

        return $this->fetch();
    }

    public function topic(){
        // 数据列表
        $data_list = TopicModel::order('create_time desc,id desc')->paginate();
        cookie('__forward__', $_SERVER['REQUEST_URI']);

        $this->assign('meta_title', '专题页管理');
        $this->assign('list', $data_list);
        return $this->fetch();
    }
    /**
     * 新增专题页
     * @author 唐小虎 <1731050423@qq.com>
     * @return mixed|void
     */
    public function add_topic()
    {
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Topic');
            // 验证失败 输出错误信息
            if(true !== $result) return $this->error($result);
            // 添加数据
            if ($article = TopicModel::create($data)) {
                // 记录行为
                action_log('article_update', 'topic', $article['id'], UID);
                return $this->success('新增成功', cookie('__forward__'));
            } else {
                return $this->error('新增失败');
            }
        }
        $this->assign('meta_title', '新增专题页');
        return $this->fetch();
    }

    public function edit_topic($id = '0')
    {
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Topic');
            // 验证失败 输出错误信息
            if (true !== $result) {
                return $this->error($result);
            }

            if ($article = TopicModel::update($data)) {               
                // Cache::clear();
                // 记录行为
                action_log('article_update', 'topic', $article['id'], UID);
                $this->success('操作成功', cookie('__forward__'));
            } else {
                $this->error('操作失败', cookie('__forward__'));
            }
        }
        $this->assign('meta_title', '编辑专题页');
        $info = TopicModel::get($id);
        $this->assign('info', $info);

        return $this->fetch();
    }
}
